<?php

namespace app\store\controller;

use app\common\model\addons\ModulesBindings;
use app\store\model\addons\Modules as ModulesModel;
use think\Request;
use think\Db;
/**
 * 后台首页
 * Class Index
 * @package app\store\controller
 */
class Addons extends Controller
{
    /**
     * 后台首页
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function index($modules='')
    {
        $request = Request::instance();
        $domain = $request->domain();
        session("modulename",$modules);
        $modules_detail = ModulesModel::detail(array('identifie'=>$modules));
        session('modules_detail',$modules_detail);
        $model = new ModulesBindings;
        $cover_list = $model->where(array('module'=>$modules,'entry'=>'cover'))->select()->toArray();
        foreach ($cover_list as &$item){
            $item['url'] = $domain.'/'.url('mobile',array('do'=>$item['do'],'module'=>$modules));
            unset($item);
        }
        $data = array();
        $data['views_total'] = Db::name('view_log')->count();
        $data['user_total'] = Db::name('store_user')->count();
        $data['pay_total'] = Db::name('wxapp')->count();
        $data['comment_total'] = Db::name('modules')->count();
        $data['coverlist'] = $cover_list;
        return $this->fetch('index', ['data' => $data]);
    }
    public function mobile(){
        //加载微擎参数
        global $_W;
        $iscall = session('iscall');
        $param = input();
        if(!isset($param['module'])){
            return $this->renderError('模块不存在请重新进入');
        }
        //获取模块标识
        $module = $param['module'];
        if(!isset($param['do'])){
            return $this->renderError('操作为空请重新进入');
        }
        $request = Request::instance();
        $domain = $request->domain();
        $_W['admin'] = $this->store;
        $_W['menus'] = $this->menus();
        $_W['siteroot'] = $domain;
        if(!$iscall){
            require_once WEB_PATH.'/extend/we/loader.class.php';
            require_once WEB_PATH.'/extend/we/WeModuleSite.class.php';
            if(empty($_W['uniacid'])){
                $_W['uniacid'] = $_W['admin']['wxapp']['wxapp_id'];
            }

            if(!file_exists(ADDONS_PATH.$module.'/site.php')){
                return $this->renderError('模块site.php不存在请重新进入');
            }
            require ADDONS_PATH.$module.'/site.php';



        }
        $modules_detail = ModulesModel::detail(array('identifie'=>$module))->toArray();
        $_W['modules'] = $modules_detail;
        session("modulename",$module);
        $module_class = $module."ModuleSite";
        $module_class = "\\".ucwords($module_class);
        $site = new $module_class();
        $do = $param['do'];
        $do = "doMobile".$do;
        $site->$do();

    }
    public function run(){
       //加载微擎参数
        global $_W;
        $iscall = session('iscall');
        $param = input();
        if(!isset($param['module'])){
            return $this->renderError('模块不存在请重新进入');
        }
        //获取模块标识
        $module = $param['module'];
        if(!isset($param['do'])){
            return $this->renderError('操作为空请重新进入');
        }
        $request = Request::instance();
        $domain = $request->domain();
        $_W['admin'] = $this->store;
        $_W['menus'] = $this->menus();
        $_W['siteroot'] = $domain;
        if(empty($_W['uniacid'])||!isset($_W['uniacid'])){
            $_W['uniacid'] = session('uniacid');
        }
        if(!$iscall){
            require_once WEB_PATH.'/extend/we/loader.class.php';
            require_once WEB_PATH.'/extend/we/WeModuleSite.class.php';
            if(empty($_W['uniacid'])){
                $_W['uniacid'] = $_W['admin']['wxapp']['wxapp_id'];
            }

            if(!file_exists(ADDONS_PATH.$module.'/site.php')){
                return $this->renderError('模块site.php不存在请重新进入');
            }
            require ADDONS_PATH.$module.'/site.php';



        }
        $modules_detail = ModulesModel::detail(array('identifie'=>$module))->toArray();
        $_W['modules'] = $modules_detail;
        session("modulename",$module);
        $module_class = $module."ModuleSite";
        $module_class = "\\".ucwords($module_class);
        $site = new $module_class();
        $do = $param['do'];
        $do = "doWeb".$do;
        $site->$do();

    }




}
